<?php

namespace Log;
use Util\Util as u;

class Logger {
	private $name;
	private $handlers = array ();
	
	private function logRecord (LogLevel $level, $msg, $class, $method, $file, $line) {
		$record = new LogRecord($level, $msg, $class, $method, $file, $line);
		$record->setLogger($this);

		$this->handle($record);
	}
	
	private function logRecordByDeduction ($caller, LogLevel $level, $msg) {		
		$class = isset($caller['class'])? $caller['class'] : null;
		$method = isset($caller['function'])? $caller['function'] : null;
		$file = $caller['file'];
		$line = $caller['line'];

		$this->logRecord($level, $msg, $class, $method, $file, $line);
	}
	
	public function __construct ($name) {
		$this->name = $name;
	}

	public function getName () {
		return $this->name;
	}

	public function addHandler (LogHandler $handler) {
		$this->handlers[] = $handler;
	}

	public function handle (LogRecord $record) {
		foreach ($this->handlers as $handler)
			$handler->logRecord($record);
	}
	
	public function log (LogLevel $level, $msg) {
		$this->logRecordByDeduction(u::getCaller(), $level, $msg);
	}
	
	public function finest ($msg) {
		$this->logRecordByDeduction(u::getCaller(), LogLevel::finest(), $msg);
	}

	public function finer ($msg) {
		$this->logRecordByDeduction(u::getCaller(), LogLevel::finer(), $msg);
	}

	public function fine ($msg) {
		$this->logRecordByDeduction(u::getCaller(), LogLevel::fine(), $msg);
	}

	public function info ($msg) {
		$this->logRecordByDeduction(u::getCaller(), LogLevel::info(), $msg);
	}

	public function warn ($msg) {
		$this->logRecordByDeduction(u::getCaller(), LogLevel::warn(), $msg);
	}

	public function critical ($msg) {
		$this->logRecordByDeduction(u::getCaller(), LogLevel::critical(), $msg);
	}

	public function fatal ($msg) {
		$this->logRecordByDeduction(u::getCaller(), LogLevel::fatal(), $msg);
	}
}
